package mac5855.app.search;

import mac5855.app.dao.DAOFactory;
import mac5855.app.lucene.search.SearchEngine;
import mac5855.app.models.Pergunta;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hit;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.Iterator;

public class HelloLucene {
  public static void main(String[] args) throws IOException, ParseException {
    StandardAnalyzer analyzer = new StandardAnalyzer();

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            DAOFactory.path );
    DAOFactory daoFactory = ( (DAOFactory) context.getBean( "daoFactory" ) );
    
    
    Pergunta[] perguntas = daoFactory.getPerguntaDAO().findAll();
 // 1. create the index
    

    // the boolean arg in the IndexWriter ctor means to
    // create a new index, overwriting any existing index
    IndexWriter writer = new IndexWriter("index-writer", analyzer, true);
    for(Pergunta pergunta : perguntas) {
    	Document doc = new Document();
        doc.add(new Field("id",pergunta.getId().toString(),Field.Store.YES,Field.Index.TOKENIZED));
        doc.add(new Field("titulo", pergunta.getTitulo(), Field.Store.YES, Field.Index.TOKENIZED));  
        // ... repeat for each column in result set
        writer.addDocument(doc);
    }
    
    writer.close();
        
    

    
    // 1. create the index
    

    // the boolean arg in the IndexWriter ctor means to
    // create a new index, overwriting any existing index
    

    // 2. query
    String querystr = args.length > 0 ? args[0] : "outra..";

    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query q = new QueryParser("titulo", analyzer).parse(querystr);

    // 3. search
    int hitsPerPage = 10;
    IndexSearcher searcher = new IndexSearcher("index-writer");
    TopDocCollector collector = new TopDocCollector(hitsPerPage);
    searcher.search(q, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    
    // 4. display results
    System.out.println("Found " + hits.length + " hits.");
    for(int i=0;i<hits.length;++i) {
      int docId = hits[i].doc;
      Document d = searcher.doc(docId);
      System.out.println((i + 1) + ". " + d.get("titulo"));
    }

    // searcher can only be closed when there
    // is no need to access the documents any more. 
    searcher.close();
    
  }

  private static void addDoc(IndexWriter w, String value) throws IOException {
    Document doc = new Document();
    doc.add(new Field("title", value, Field.Store.YES, Field.Index.TOKENIZED));
    w.addDocument(doc);
  }
}
